/*
 * FilePath     : \src\stores\modules\session.ts
 * Author       : 苏军志
 * Date         : 2024-01-16 08:32
 * LastEditors  : 郭鹏超
 * LastEditTime : 2025-01-24 17:30
 * Description  : session状态管理
 * CodeIterationRecord:
 */
import { defineStore } from "pinia";

const useSessionStore = defineStore({
  id: "session",
  state: () => {
    return {
      // 本地时间和服务器时间间隔毫秒数
      timeDiff: 0,
      // 登录状态
      isLogin: false,
      // 加载状态 等于0隐藏loading 大于0显示loading
      loading: 0,
      loadingText: common.session("session")?.loadingText,
      isRouterAlive: true,
      // 登录Token
      authorization: "",
      expiresDate: undefined,
      contentFullScreen: false
    };
  },
  // 数据持久化
  persist: [
    {
      storage: sessionStorage
    }
  ],
  actions: {
    setSession(sessionInfo: Record<string, any>) {
      const { authorization, expiresDate, serverDateTime } = sessionInfo;
      this.expiresDate = expiresDate;
      this.authorization = authorization;
      this.isLogin = !!authorization;
      this.timeDiff = serverDateTime ? new Date(serverDateTime).getTime() + 50 - (Date as any).localDate().getTime() : 0;
    },
    /**
     * @description: 页面内容重载
     * @param {number} delay - 延迟毫秒数
     * @return {*}
     */
    async reloadPage(delay = 600) {
      this.isRouterAlive = false;
      if (delay) {
        setTimeout(() => {
          this.isRouterAlive = true;
        }, delay);
      } else {
        this.isRouterAlive = true;
      }
    }
  }
});

export default useSessionStore;
